package backstage.app.controller.pt;

import java.time.LocalDateTime;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import backstage.app.entity.NoConstionParent;
import backstage.app.entity.NoConstitution;
import backstage.app.mapper.NoConstionParentMapper;
import backstage.app.mapper.NoConstitutionMapper;
import backstage.app.respont.RespontDateJson;
import backstage.app.service.impl.util.NoConstitutionImplUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("${spring.application.name}/no-constitution/api")
@Api("非学员体质测试-控制器")
public class NoConstitutionController {
	 
	@Autowired
	private NoConstitutionMapper noConstitutionMapper;
	@Autowired
	private NoConstionParentMapper noConstionParentMapper;
	@Autowired
	private NoConstitutionImplUtil noConstitutionImplUtil;
	 private static final Logger logger = (Logger) LoggerFactory.getLogger(NoConstitutionController.class);
	
	@PostMapping("/listBySid")
	@ApiOperation(value="根据sid查询")
	@ApiImplicitParams({
		@ApiImplicitParam(value="sid")
	})
	public RespontDateJson<?>listBySid(@RequestParam(value="sid",required = true)Integer sid){
		List<NoConstitution>list=null;
		try {
			 list=noConstitutionMapper.selectBySid(sid);
			 for (NoConstitution constitution : list) {
				 NoConstionParent constionParent = noConstionParentMapper.selectByPrimaryKey(constitution.getCpId());
				 constitution.setConstionParent(constionParent);
			}	 
		} catch (Exception e) {
			logger.error("查询体质测试时报错: {}",sid);
		}
		return RespontDateJson.ok(list, "成功");
	}
	
	
	
	
	@PostMapping("/deleteByCid")
	@ApiOperation(value="根据cid删除")
	@ApiImplicitParams({
		@ApiImplicitParam(value="cid")
	})
	public RespontDateJson<?>deleteByCid(@RequestParam(value="cid",required = true)Integer cid){
		 int result = -1;
		try {
			result=noConstitutionMapper.deleteByPrimaryKey(cid);
			return RespontDateJson.ok(result, "成功");
		} catch (Exception e) {
			logger.error("删除体质测试时报错: {}",result);
			return RespontDateJson.ok(result, "删除体质测试时报错");
		}
		 
		 
	}
	
	@PostMapping("/selectByCid")
	@ApiOperation(value="根据cid查询")
	@ApiImplicitParams({
		@ApiImplicitParam(value="cid")
	})
	public RespontDateJson<?>selectByCid(@RequestParam(value="cid",required = true)Integer cid){
		NoConstitution result = null;
		try {	 
			result=noConstitutionMapper.selectByPrimaryKey(cid);
			NoConstionParent constionParent = noConstionParentMapper.selectByPrimaryKey(result.getCpId());
			result.setConstionParent(constionParent);				 
			return RespontDateJson.ok(result, "成功");			 
		} catch (Exception e) {
			logger.error("查询体质测试时报错: {}",result);
			return RespontDateJson.ok(result, "查询体质测试时报错");
		}
		 
		 
	}
	 
	
	
	@PostMapping("/selectConstionParentByCid")
	@ApiOperation(value="根据cid查询")
	@ApiImplicitParams({
		@ApiImplicitParam(value="cid")
	})
	public RespontDateJson<?>selectConstionParentByCid(@RequestParam(value="cid",required = true)Integer cid){
		NoConstitution result = null;
		try {	 
			result=noConstitutionMapper.selectByPrimaryKey(cid);
			NoConstionParent constionParent = noConstionParentMapper.selectByPrimaryKey(result.getCpId());
			result=noConstitutionImplUtil.getConstitution1(result);
			result.setConstionParent(constionParent);				 
			return RespontDateJson.ok(result, "成功");			 
		} catch (Exception e) {
			logger.error("查询体质测试时报错: {}",result);
			return RespontDateJson.ok(result, "查询体质测试时报错");
		}
		 
		 
	}
 
	@PostMapping("/insert")
	@ApiOperation(value="添加")
	@ApiImplicitParams({
		@ApiImplicitParam(value="conditionType类型技能")
	})
	public RespontDateJson<?>insert(
			@RequestParam(value="sid",required = true)Integer sid,
			//学生对应id
			@RequestParam(value="conditionType",required = false)Integer conditionType,
			@RequestParam(value="height",required = false)double height,
			@RequestParam(value="width",required = false)double width,
			@RequestParam(value="abduction",required = false)double abduction,
			@RequestParam(value="fingerSpan",required = false)double fingerSpan,
			//大文本
			@RequestParam(value="singForward",required = false)double singForward,
			@RequestParam(value="roundTrip",required = false)Integer roundTrip,
			@RequestParam(value="courtSprint",required = false)double courtSprint,
			@RequestParam(value="restrtedArea",required = false)double restrtedArea,
			@RequestParam(value="klingWing",required = false)double klingWing,
			@RequestParam(value="verticalJump",required = false)Integer verticalJump,
			@RequestParam(value="continJump",required = false)Integer continJump,
			//备注
			@RequestParam(value="remake",required = false)String remake){
		
		NoConstitution constitution=new NoConstitution();
		constitution.setSid(sid);
		constitution.setHeight(height);
		//对应
		constitution.setCpId(conditionType);
		constitution.setWeight(width);
		constitution.setAbduction(abduction);
		constitution.setFingerSpan(fingerSpan);
		constitution.setSingForward(singForward);
		constitution.setRoundTrip(roundTrip);
		constitution.setCourtSprint(courtSprint);
		constitution.setRestrtedArea(restrtedArea);
		constitution.setKlingWing(klingWing);
		constitution.setVerticalJump(verticalJump);
		constitution.setContiNump(continJump);
		constitution.setRemake(remake);
		constitution.setCreateTime(LocalDateTime.now());
		constitution.setGenerateTime(LocalDateTime.now());
		constitution.setUpdateTime(LocalDateTime.now());
		int result=-1;
		try {
			  result=noConstitutionMapper.insert(constitution);
			  return result>0?RespontDateJson.ok(result, "添加成功"):RespontDateJson.fuall(result,"添加失败，出现未知异常，请联系开发人员");
		} catch (Exception e) {
			logger.error("学员添加体质测试时，数据库发生异常！");
			return RespontDateJson.fuall(result, "学员添加体质测试时，数据库发生异常！");
		}
		 
		
		 
	}
	
	
	@PostMapping("/update")
	@ApiOperation(value="修改")
	@ApiImplicitParams({
		@ApiImplicitParam(value="conditionType类型技能")
	})
	public RespontDateJson<?>update(
			@RequestParam(value="cid",required = true)Integer cid,
			//学生对应id
			 
			@RequestParam(value="height",required = false)Double height,
			@RequestParam(value="width",required = false)Double width,
			@RequestParam(value="abduction",required = false)Double abduction,
			@RequestParam(value="fingerSpan",required = false)Double fingerSpan,
			//大文本
			@RequestParam(value="singForward",required = false)Double singForward,
			@RequestParam(value="roundTrip",required = false)Integer roundTrip,
			@RequestParam(value="courtSprint",required = false)Double courtSprint,
			@RequestParam(value="restrtedArea",required = false)Double restrtedArea,
			@RequestParam(value="klingWing",required = false)Double klingWing,
			@RequestParam(value="verticalJump",required = false)Integer verticalJump,
			@RequestParam(value="continJump",required = false)Integer continJump,
			//备注
			@RequestParam(value="remake",required = false)String remake){
		logger.info("进入后台");
		NoConstitution constitution=new NoConstitution();
		constitution.setCid(cid);
		constitution.setHeight(height);
		//对应
		constitution.setWeight(width);
		constitution.setAbduction(abduction);
		constitution.setFingerSpan(fingerSpan);
		constitution.setSingForward(singForward);
		constitution.setRoundTrip(roundTrip);
		constitution.setCourtSprint(courtSprint);
		constitution.setRestrtedArea(restrtedArea);
		constitution.setKlingWing(klingWing);
		constitution.setVerticalJump(verticalJump);
		constitution.setContiNump(continJump);
		 System.out.println(constitution.getContiNump());
		constitution.setRemake(remake);
		constitution.setUpdateTime(LocalDateTime.now());
	
		int result=-1;
		try {
			  result=noConstitutionMapper.updateByPrimaryKey(constitution);
			  return result>0?RespontDateJson.ok(result, "修改成功"):RespontDateJson.fuall(result,"修改失败，出现未知异常，请联系开发人员");
		} catch (Exception e) {
			logger.error("学员修改体质测试时，数据库发生异常！");
			return RespontDateJson.fuall(result, "学员修改体质测试时，数据库发生异常！");
		}
		 
		 
	}
	
}
